package com.exam.web.interceptor;

import java.lang.reflect.Method;

import javax.annotation.Resource;

import com.exam.bean.privilege.Employee;
import com.exam.bean.privilege.PrivilegeGroup;
import com.exam.bean.privilege.SystemPrivilege;
import com.exam.service.privilege.EmployeeService;
import com.exam.web.action.privilege.Privilege;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class PermissionInterceptor implements Interceptor {
	
	private static final long serialVersionUID = 7742533757699345996L;

	@Resource
	private EmployeeService employeeService;
	
	@Override
	public void destroy() {}

	@Override
	public void init() {}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		return invocation.invoke();/*
		Object username=ActionContext.getContext().getSession().get("loginName");
		
		if(username!=null&&!"".equals(username)){
			ActionProxy proxy = invocation.getProxy();
			String methodName = proxy.getMethod();
			Object action = proxy.getAction();

			try {
				Method method=action.getClass().getMethod(methodName);
				if (null != method) {
					Privilege privilege = method.getAnnotation(Privilege.class);
					if (null != privilege) {
						for(PrivilegeGroup privilegeGroup : employeeService.find(Employee.class, username).getPrivilegegroups()){
							for(SystemPrivilege systemPrivilege : privilegeGroup.getPrivileges()){
								if(systemPrivilege.getId().getModel().equals(privilege.model())&&
										systemPrivilege.getId().getPrivilegeValue().equals(privilege.privilegeValue())){
									return invocation.invoke();
								}
							}
						}
					}
					else return invocation.invoke();
				}
			}
			catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
			ActionContext.getContext().put("message", "你没有权限执行该操作");
			ActionContext.getContext().put("urladdress", "javascript:history.back()");
			return "message";
		}
		return "login";*/
	}
}
